package me.monster.smarttraffic.ui.bus;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.monster.smarttraffic.R;
import me.monster.smarttraffic.base.AbstractToolActivity;

/**
 * @description
 * @author: Created jiangjiwei in 2020-02-13 14:34
 */
public class BusActivity extends AbstractToolActivity {

    @BindView(R.id.bus_tab)
    TabLayout tabBus;
    @BindView(R.id.vp_bus_container)
    ViewPager vpBus;

    List<Fragment> fragments = new ArrayList<>(2);

    public static void start(Activity context) {
        Intent starter = new Intent(context, BusActivity.class);
        context.startActivity(starter);
        context.overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
    }

    @Override
    public View closeToolView() {
        return vpBus;
    }

    @Override
    public String getInitTitle() {
        return getString(R.string.dash_menu_b);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_bus;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragments.add(StationLeftFragment.newInstance());
        fragments.add(StationRightFragment.newInstance());
        BusAdapter busAdapter = new BusAdapter(getSupportFragmentManager(), fragments);
        vpBus.setAdapter(busAdapter);
        tabBus.setupWithViewPager(vpBus);
        tabBus.removeAllTabs();
        tabBus.addTab(tabBus.newTab().setText(R.string.title_station_left));
        tabBus.addTab(tabBus.newTab().setText(R.string.title_station_right));
        tabBus.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switchTab(tab);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void switchTab(TabLayout.Tab tab) {
        setToolTittle(tab.getPosition());
    }

    public void setToolTittle(int position) {
        toolHelper.setTitle(getString(position == 0 ? R.string.title_station_left : R.string.title_station_right));
    }
}
